using System.Reflection;
using SageKing.Features.Contracts; 
using SageKing.Studio.Contracts;

namespace SageKing.Studio.Services;

/// <inheritdoc />
public class DefaultFeatureService : IDesignerFeatureService
{
    private readonly IEnumerable<IDesignerFeature> _features;
    private readonly IServiceProvider _serviceProvider;
    private readonly IBlazorServiceAccessor _blazorServiceAccessor;

    /// <summary>
    /// Initializes a new instance of the <see cref="DefaultFeatureService"/> class.
    /// </summary>
    public DefaultFeatureService(IEnumerable<IDesignerFeature> features, IServiceProvider serviceProvider, IBlazorServiceAccessor blazorServiceAccessor)
    {
        _features = features;
        _serviceProvider = serviceProvider;
        _blazorServiceAccessor = blazorServiceAccessor;
        //_blazorServiceAccessor.Services = _serviceProvider;
    }
    
    /// <inheritdoc />
    public event Action? Initialized;

    /// <inheritdoc />
    public IEnumerable<IDesignerFeature> GetFeatures()
    {
        return _features.ToList();
    }

    /// <inheritdoc />
    public async Task InitializeFeaturesAsync(CancellationToken cancellationToken = default)
    { 

        foreach (var feature in GetFeatures())
        {
            await feature.InitializeAsync(cancellationToken);
        }
        
        OnInitialized();
    }

    private void OnInitialized()
    {
        Initialized?.Invoke();
    }
}